#!/usr/bin/env python3
from PIL import Image, ImageDraw
import sys

def read_texture_file(filename):
    with open(filename, 'r') as f:
        return [line.strip() for line in f.readlines()]

def create_texture_from_file(input_file, output_file, colors):
    # 读取纹理文件
    texture = read_texture_file(input_file)
    
    # 创建图像
    height = len(texture)
    width = len(texture[0]) if height > 0 else 0
    scale = 4  # 每个字符扩展为4x4像素
    
    img = Image.new('RGBA', (width * scale, height * scale), (0, 0, 0, 0))
    pixels = img.load()
    
    # 将文本转换为像素
    for y, line in enumerate(texture):
        for x, char in enumerate(line):
            if char != '.':  # 跳过空白区域
                color = colors.get(char, (0, 0, 0, 0))
                # 将每个字符扩展为4x4像素
                for dx in range(scale):
                    for dy in range(scale):
                        pixels[x * scale + dx, y * scale + dy] = color
    
    img.save(output_file)

def create_tank_player():
    # 创建玩家坦克纹理
    colors = {
        '1': (30, 144, 255),   # 浅蓝色
        '2': (0, 0, 180),      # 深蓝色
        '3': (65, 105, 225),   # 钢蓝色
        '#': (100, 100, 100),  # 炮管（灰色）
    }
    create_texture_from_file('resources/textures/tank_player.txt', 
                           'resources/textures/tank_player.png', 
                           colors)

def create_tank_enemy():
    # 创建敌人坦克纹理
    colors = {
        '4': (255, 99, 71),    # 浅红色
        '5': (180, 0, 0),      # 深红色
        '6': (139, 0, 0),      # 暗红色
        '#': (100, 100, 100),  # 炮管（灰色）
    }
    create_texture_from_file('resources/textures/tank_enemy.txt',
                           'resources/textures/tank_enemy.png',
                           colors)

def create_grass():
    # 创建草地纹理
    colors = {
        '4': (34, 139, 34),    # 森林绿
    }
    create_texture_from_file('resources/textures/grass.txt',
                           'resources/textures/grass.png',
                           colors)

def create_grass_flower():
    # 创建带花的草地纹理
    colors = {
        '4': (34, 139, 34),    # 森林绿
        '7': (255, 255, 0),    # 黄色花朵
    }
    create_texture_from_file('resources/textures/grass_flower.txt',
                           'resources/textures/grass_flower.png',
                           colors)

def create_ice():
    # 创建冰块纹理
    img = Image.new('RGBA', (32, 32), (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)
    
    # 绘制带圆角的白色冰块
    radius = 4  # 圆角半径
    white = (255, 255, 255, 230)  # 半透明白色
    light_blue = (200, 220, 255, 230)  # 淡蓝色边缘
    
    # 绘制主体
    draw.rectangle([radius, 0, 31-radius, 31], fill=white)  # 上下矩形
    draw.rectangle([0, radius, 31, 31-radius], fill=white)  # 左右矩形
    
    # 绘制四个圆角
    draw.pieslice([0, 0, radius*2, radius*2], 180, 270, fill=white)  # 左上
    draw.pieslice([31-radius*2, 0, 31, radius*2], 270, 0, fill=white)  # 右上
    draw.pieslice([0, 31-radius*2, radius*2, 31], 90, 180, fill=white)  # 左下
    draw.pieslice([31-radius*2, 31-radius*2, 31, 31], 0, 90, fill=white)  # 右下
    
    # 添加淡蓝色边缘效果
    draw.line([radius, 0, 31-radius, 0], fill=light_blue, width=1)  # 上边
    draw.line([0, radius, 0, 31-radius], fill=light_blue, width=1)  # 左边
    
    img.save('resources/textures/ice.png')

def create_bullet():
    # 创建子弹纹理（黄色）
    img = Image.new('RGBA', (8, 8), (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)
    
    # 圆形子弹
    draw.ellipse([0, 0, 7, 7], fill=(255, 255, 0))
    
    img.save('resources/textures/bullet.png')

def create_wall():
    # 创建墙体纹理（灰色）
    img = Image.new('RGBA', (32, 32), (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)
    
    # 实心墙块
    draw.rectangle([0, 0, 31, 31], fill=(128, 128, 128))
    # 添加一些纹理细节
    for i in range(0, 32, 8):
        for j in range(0, 32, 8):
            draw.rectangle([i, j, i+7, j+7], outline=(100, 100, 100))
    
    img.save('resources/textures/wall.png')

def create_wall_destructible():
    # 创建可破坏墙体纹理（棕色）
    img = Image.new('RGBA', (32, 32), (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)
    
    # 砖块图案
    for i in range(0, 32, 8):
        for j in range(0, 32, 4):
            if (j // 4) % 2 == 0:
                draw.rectangle([i, j, i+7, j+3], fill=(139, 69, 19))
            else:
                if i < 24:  # 避免最后一列溢出
                    draw.rectangle([i+4, j, i+11, j+3], fill=(139, 69, 19))
    
    img.save('resources/textures/wall_destructible.png')

def create_explosion():
    # 创建爆炸动画纹理（橙红色）
    img = Image.new('RGBA', (64, 64), (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)
    
    # 创建爆炸效果（同心圆）
    colors = [
        (255, 69, 0),    # 橙红色
        (255, 140, 0),   # 深橙色
        (255, 215, 0)    # 金色
    ]
    
    # 从外到内画三个圆
    for i, color in enumerate(colors):
        radius = 30 - i * 8
        draw.ellipse([32-radius, 32-radius, 32+radius, 32+radius], fill=color)
    
    img.save('resources/textures/explosion.png')

def create_strong_wall():
    # 创建强化砖块纹理（红色外框，白色中层，棕色内层）
    img = Image.new('RGBA', (32, 32), (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)
    
    # 颜色定义
    red = (255, 0, 0)        # 红色外框
    white = (255, 255, 255)  # 白色中层
    brown = (139, 69, 19)    # 棕色内层
    
    # 绘制三层嵌套的方块
    draw.rectangle([0, 0, 31, 31], fill=red)         # 外层红色
    draw.rectangle([4, 4, 27, 27], fill=white)       # 中层白色
    draw.rectangle([8, 8, 23, 23], fill=brown)       # 内层棕色
    
    img.save('resources/textures/strong_wall.png')

def create_strong_wall_damaged():
    # 创建受损的强化砖块纹理（白色外框，棕色内层）
    img = Image.new('RGBA', (32, 32), (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)
    
    # 颜色定义
    white = (255, 255, 255)  # 白色外框
    brown = (139, 69, 19)    # 棕色内层
    
    # 绘制两层嵌套的方块
    draw.rectangle([4, 4, 27, 27], fill=white)  # 外层白色
    draw.rectangle([8, 8, 23, 23], fill=brown)  # 内层棕色
    
    img.save('resources/textures/strong_wall_damaged.png')

if __name__ == '__main__':
    if len(sys.argv) == 3:
        # 如果提供了输入和输出文件参数，只创建指定的纹理
        input_file = sys.argv[1]
        output_file = sys.argv[2]
        if 'grass' in input_file:
            colors = {'4': (34, 139, 34)}  # 森林绿
        else:
            colors = {}  # 默认颜色映射
        create_texture_from_file(input_file, output_file, colors)
    else:
        # 否则创建所有纹理
        create_tank_player()
        create_tank_enemy()
        create_bullet()
        create_wall()
        create_wall_destructible()
        create_explosion()
        create_grass()
        create_grass_flower()
        create_ice()
        create_strong_wall()
        create_strong_wall_damaged()
    print("所有纹理文件已生成完成！")